package com.github.pig.sales.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pig.admin.model.entity.SysUser;
import com.github.pig.admin.service.SysUserService;
import com.github.pig.common.util.Query;
import com.github.pig.common.util.R;
import com.github.pig.common.vo.UserVO;
import com.github.pig.sales.service.ISalesPublicRepertoryManageService;
import com.github.pig.sales.service.ISalesPublicRepertoryService;
import com.github.pig.sales.service.impl.SalesPublicRepertoryManageServiceImpl;
import com.github.pig.training.entity.TrainingStudent;
import com.github.pig.training.service.ITrainingStudentService;
import com.github.pig.training.util.ReadData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.plugins.Page;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/salesPublicRepertoryManage")
public class SalesPublicRepertoryManageController {
    @Autowired private ITrainingStudentService trainingStudentService;
    @Autowired private SysUserService userService ;
    @Autowired private ISalesPublicRepertoryManageService iSalesPublicRepertoryManageService;


    @RequestMapping("/page")
    public Page page(@RequestParam Map<String,Object> params){
        return iSalesPublicRepertoryManageService.getList(params);
    }

    /**
     * 领取学员
     * @param fid
     * @param userVo
     * @return
     */
    @RequestMapping("/sFollowUp")
    public R<Boolean> sFollowUp(@RequestParam int fid,UserVO userVo){
//        EntityWrapper<SysUser> ew1 = new EntityWrapper<>();
//        ew1.setSqlSelect("user_id","name");
//        List<SysUser> users = userService.selectList(ew1);
//        try {
//            List<TrainingStudent> list = ReadData.setTrainingStudent(users);
//            for(TrainingStudent t :list){
//                trainingStudentService.insert(t);
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return null;
        return new R(trainingStudentService.sFollowUp(fid,userVo.getUsername()));
    }


    @PutMapping("/updateToBatch/{followUpUser}")
    public R<Boolean> updateToBatch(@RequestBody List<TrainingStudent> list,@PathVariable Integer followUpUser,UserVO user){
        Integer userId = userService.findUserByUsername(user.getUsername()).getUserId();
        for (int i = 0; i < list.size(); i++) {
            if (followUpUser == -1){
                list.get(i).setFollowUpUser(userId);
            }else {
                list.get(i).setFollowUpUser(followUpUser);
            }
            list.get(i).setUpdateTime(new Date());
            list.get(i).setFollowUpStatus("1601");
            list.get(i).setUpdateUser(userId);
        }
        return new R<>(trainingStudentService.updateBatchById(list));
    }

}
